package com.jjw.springbootcvs.config;

import com.jjw.springbootcvs.interceptor.LoginInterceptor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.*;

import java.util.Arrays;

/**
 * @author xiaojuan
 * @Date 2024/11/26
 * @Description
 */
@Configuration
@ComponentScan("com.jjw.springbootcvs")
@RequiredArgsConstructor
@Slf4j
public class MvcConfig implements WebMvcConfigurer {

    private final BootCvsProperties bootCvsProperties;

    private final LoginInterceptor loginInterceptor;

    /**
     * 配置静态资源路径
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置静态资源路径
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }

    /**
     * 配置默认页
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        log.info("配置视图解析器....{}",bootCvsProperties.getViewMappings());
        bootCvsProperties.getViewMappings().forEach((k,v)->{
            String key = k.replace("1", "/");
            registry.addViewController(key).setViewName(v);
        });
    }

    /**
     * 配置拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] exclude = bootCvsProperties.getExclude();
        log.info("自定义拦截器配置...{}", Arrays.toString(exclude));
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(exclude);
    }
}
